package com.ddyx.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import com.ddyx.entity.Booking;
import com.ddyx.entity.Customer;
import com.ddyx.entity.EventClickFavorite;
import com.ddyx.entity.EventClickLike;
import com.ddyx.entity.EventClickPageview;
import com.ddyx.entity.Goods;
import com.ddyx.mapper.BookingMapper;
import com.ddyx.mapper.CustomerMapper;
import com.ddyx.mapper.EventClickFavoriteMapper;
import com.ddyx.mapper.EventClickLikeMapper;
import com.ddyx.mapper.EventClickPageviewMapper;
import com.ddyx.mapper.GoodsMapper;
import com.ddyx.service.IOperateService;
import com.ddyx.util.CalculateUtil;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.ddyx.vo.ManagerCircleVisitorData;
import com.ddyx.vo.ManagerCusotmerData;
import com.ddyx.vo.ManagerProductData;
import com.ddyx.vo.ManagerTopData;
import com.ddyx.vo.ManagerVisitorData;
import com.ddyx.vo.StoreReportVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Slf4j
@Service
public class OperateServiceImpl implements IOperateService {
	@Resource
	EventClickPageviewMapper eventClickPageviewMapper;
	@Resource
	EventClickFavoriteMapper eventClickFavoriteMapper;
	@Resource
	EventClickLikeMapper eventClickLikeMapper;
	@Resource
	BookingMapper bookingMapper;
	@Resource
	CustomerMapper customerMapper;
	@Resource
	GoodsMapper goodsMapper;
	
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getManagerTopData(RequestForm param) {
		ResponseForm result = new ResponseForm();
		ManagerTopData data = new ManagerTopData();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String storeNo = (String) paramMap.get("storeNo");
			int storePvTotal = this.getStorePvTotal(storeNo);
			int storeFavorTotal = this.getStoreFavorTotal(storeNo);
			int storeLikeTotal = this.getStoreLikeTotal(storeNo);
			int storeBookingTotal = this.getStoreBookingTotal(storeNo);
			data.setStorePvTotal(storePvTotal);
			data.setStoreFavorTotal(storeFavorTotal);
			data.setStoreLikeTotal(storeLikeTotal);
			data.setStoreBookingTotal(storeBookingTotal);
			result.setData(data);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getManagerCircleVisitorData(RequestForm param) {
		ResponseForm result = new ResponseForm();
		ManagerCircleVisitorData data = new ManagerCircleVisitorData(); 
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String storeNo = (String) paramMap.get("storeNo");
			Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) paramMap.get("startDate"));
			Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) paramMap.get("endDate"));
			int storeUvCount  = this.getStoreUvCount(storeNo,startDate,endDate);
			int bookingUserCount = this.getBookingUserCount(storeNo, startDate, endDate);
			double userRepeatRate = this.getUserRepeatRate(storeNo, startDate, endDate);
			data.setStoreUvCount(storeUvCount);
			data.setBookingUserCount(bookingUserCount);
			data.setUserRepeatRate(userRepeatRate);
			result.setData(data);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getManagerVisitorData(RequestForm param) {
		ResponseForm result = new ResponseForm();
		ManagerVisitorData data = new ManagerVisitorData(); 
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String storeNo = (String) paramMap.get("storeNo");
			Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) paramMap.get("startDate"));
			Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) paramMap.get("endDate"));
			int storeUvCount = this.getStoreUvCount(storeNo, startDate, endDate);
			int storePvCount = this.getStorePvCount(storeNo, startDate, endDate);
			data.setStorePvCount(storePvCount);
			data.setStoreUvCount(storeUvCount);
			result.setData(data);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getManagerProductData(RequestForm param) {
		ResponseForm result = new ResponseForm();
		ManagerProductData data = new ManagerProductData(); 
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String storeNo = (String) paramMap.get("storeNo");
			Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) paramMap.get("startDate"));
			Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) paramMap.get("endDate"));
			int storeGoodsTotal = this.getStoreGoodsTotal(storeNo);
			String goodsPv2bkRate = this.getGoodsPv2bkRate(storeNo, startDate, endDate);
			data.setStoreGoodsTotal(storeGoodsTotal);
			data.setGoodsPv2bkRate(goodsPv2bkRate);
			result.setData(data);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getManagerCusotmerData(RequestForm param) {
		ResponseForm result = new ResponseForm();
		ManagerCusotmerData data = new ManagerCusotmerData(); 
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String storeNo = (String) paramMap.get("storeNo");
			Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) paramMap.get("startDate"));
			Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) paramMap.get("endDate"));
			int userAddedCount = this.getuserAddedCount(storeNo, startDate, endDate);
			int userTotalCount = this.getuserTotalCount(storeNo);
			data.setUserAddedCount(userAddedCount);
			data.setUserTotalCount(userTotalCount);
			result.setData(data);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}
	
	/**
	 * 获取页面顶部基础数据
	 * 小店浏览总数 storePvTotal int(20)
	 */
	private int getStorePvTotal(String storeNo) {
		EventClickPageview eventClickPageview = new EventClickPageview();
		eventClickPageview.setStoreNo(storeNo);
		return eventClickPageviewMapper.selectCount(eventClickPageview);
	}

	/**
	 * 获取页面顶部基础数据
	 * 收藏商品总数 storeFavorTotal int(20)
	 */
	private int getStoreFavorTotal(String storeNo) {
		EventClickFavorite eventClickFavorite = new EventClickFavorite();
		eventClickFavorite.setStoreNo(storeNo);
		return eventClickFavoriteMapper.selectCount(eventClickFavorite);
	}

	/**
	 * 获取页面顶部基础数据
	 * 点赞商品总数 storeLikeTotal int(20)
	 */
	private int getStoreLikeTotal(String storeNo) {
		EventClickLike eventClickLike = new EventClickLike();
		eventClickLike.setStoreNo(storeNo);
		return eventClickLikeMapper.selectCount(eventClickLike);
	}

	/**
	 * 获取页面顶部基础数据
	 * 预约单总数 storeBookingTotal int(20)
	 */
	private int getStoreBookingTotal(String storeNo) {
		Booking booking = new Booking();
		booking.setStoreNo(storeNo);
		return bookingMapper.selectCount(booking);
	}
	
	/**
	 * 7日/30日 的数据统计
	 * 访问小店的用户数 storeUvCount int(20)
	 */
	private int getStoreUvCount(String storeNo,Date startDate,Date endDate) {
		Example example = new Example(EventClickPageview.class);
		example.selectProperties("wxUid");
		example.setDistinct(true);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("storeNo", storeNo);
		criteria.andBetween("eventTime", startDate, endDate);
		List<EventClickPageview> list = eventClickPageviewMapper.selectByExample(example);
		return list.size();
	}
	/**
	 * 7日/30日 的数据统计
	 * 预约的用户数 bookingUserCount int(20)
	 */
	private int getBookingUserCount(String storeNo,Date startDate,Date endDate) {
		Example example = new Example(Booking.class);
		example.selectProperties("wxUid");
		example.setDistinct(true);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("storeNo", storeNo);
		criteria.andBetween("createTime", startDate, endDate);
		List<Booking> list = bookingMapper.selectByExample(example);
		return list.size();
	}
	/**
	 * 7日/30日 的数据统计
	 * 回头率 userRepeatRate double(4,2)
	 */
	private double getUserRepeatRate(String storeNo,Date startDate,Date endDate) {
		return 32.5;
	}

	/**
	 * 访客数据统计
	 * 昨日访问小店的用户数 storeUvCount int(20)
	 */
	
	/**
	 * 访客数据统计
	 * 昨日小店浏览数 storePvCount int(20)
	 */
	private int getStorePvCount(String storeNo,Date startDate,Date endDate) {
		Example example = new Example(EventClickPageview.class);
		Criteria criteria = example.createCriteria();
		criteria.andBetween("eventTime", startDate, endDate);
		criteria.andEqualTo("storeNo", storeNo);
		return eventClickPageviewMapper.selectCountByExample(example);
	}
	
	/**
	 *	商品数据统计
	 *	小店商品总数 storeGoodsTotal int(20)
	 */
	private int getStoreGoodsTotal(String storeNo) {
		Goods goods = new Goods();
		goods.setStoreNo(storeNo);
		return goodsMapper.selectCount(goods);
	}
	
	/**
	 *	商品数据统计
	 *	30日浏览-预约商品转化率 goodsPv2bkRate double(4,2)
	 */
	private String getGoodsPv2bkRate(String storeNo,Date startDate,Date endDate) {
		Example example = new Example(EventClickPageview.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("storeNo", storeNo);
		criteria.andBetween("eventTime", startDate, endDate);
		int storePvCount = eventClickPageviewMapper.selectCountByExample(example);
		
		Example example2 = new Example(Booking.class);
		Criteria criteria2 = example2.createCriteria();
		criteria2.andEqualTo("storeNo", storeNo);
		criteria2.andBetween("createTime", startDate, endDate);
		int storeBookingTotal = bookingMapper.selectCountByExample(example2);

		String goodsPv2bkRate = CalculateUtil.getGoodsPv2bkRate(storeBookingTotal,storePvCount);
		return goodsPv2bkRate;
	}

	/**
	 *	 客户数据统计
	 *	新增客户（人）userAddedCount int(20)
	 */
	private int getuserAddedCount(String storeNo,Date startDate,Date endDate) {
		Example example = new Example(Customer.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("storeNo", storeNo);
		criteria.andBetween("createTime", startDate, endDate);
		return customerMapper.selectCountByExample(example);
	}
	/**
	 *	 客户数据统计
	 *	客户总数（人）userTotalCount int(20)
	 */
	private int getuserTotalCount(String storeNo) {
		Customer customer = new Customer();
		customer.setStoreNo(storeNo);
		return customerMapper.selectCount(customer);
	}
	
}
